package com.jizi.machine.controller;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jizi.machine.service.MachineService;
import com.jizi.machine.vo.RobotVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/TimedTask")
public class TimedTask {

    @Value("${webhook}")
    private String webhook;

    @Autowired
    private MachineService machineService;

    @GetMapping("/test")
    @Scheduled(cron = "0 0 10 * * ? ")
    public void test() throws IOException {
        String url = "https://api.xwteam.cn/api/wechat/step?key=McTbJHLZcgskInVGJBRjdjSXhR&phone=13676084606&password=1999h1028&steps=66666";
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("key", "McTbJHLZcgskInVGJBRjdjSXhR");
        paramMap.put("phone", "13676084606");
        paramMap.put("password", "1999h1028");
        paramMap.put("steps", "66666");
        String result2 = HttpRequest.get(url)
                .header(Header.USER_AGENT, "Hutool http")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        //result2转成JSON
        JSONObject result = JSONUtil.parseObj(result2);
        log.info("result:{}", result);
        String data = String.valueOf(result.get("code"));
        if (!"200".equals(data)) {
            return;
        }

        //VO这段可以写在业务逻辑层
        RobotVo vo = new RobotVo();
        //机器人地址
        vo.setWebhookAddress(webhook);
        //1.第一种情况：发送文本消息
        vo.setContent("微信步数刷新成功！");
        List<String> memberList = new ArrayList<>();
        //memberList.add("@all");
        vo.setMemberList(memberList);
        vo.setMsgType("text");

        machineService.run(vo);
    }

    /**
     * 发送邮件信息
     */
    @GetMapping("/sendEmail")
    public String sendEmail() {
        MailUtil.send("1678846945@qq.com", "新上架商品", "contents", false);
        return "成功";
    }

}

